﻿#region imports

using System;
using System.Collections.Generic;
using Codeplex.ODataTFS.Model.Entities;
using Codeplex.ODataTFS.Model.ExpressionVisitors;
using Codeplex.ODataTFS.Model.Serialization;
using Microsoft.Data.Services.Toolkit.QueryModel;

#endregion

namespace Codeplex.ODataTFS.Model.Repositories
{
	public class BuildRepository
	{
		private readonly TFSBuildProxy _proxy;

		public BuildRepository(TFSBuildProxy proxy)
		{
			_proxy = proxy;
		}

		public Build GetOne(string project, string definition, string number)
		{
			return _proxy.GetBuild(project, definition, number);
		}

		[RepositoryBehavior(HandlesFilter = true)]
		public IEnumerable<Build> GetBuildsByProject(ODataSelectManyQueryOperation operation)
		{
			if (operation == null)
			{
				throw new ArgumentNullException("operation");
			}

			var parameters = new BuildFilterExpressionVisitor(operation.FilterExpression).Eval();
			return _proxy.GetBuildsByProject(operation.Key, parameters);
		}

		[RepositoryBehavior(HandlesFilter = true)]
		public IEnumerable<Build> GetAll(ODataQueryOperation operation)
		{
			if (operation == null)
			{
				throw new ArgumentNullException("operation");
			}

			var parameters = new BuildFilterExpressionVisitor(operation.FilterExpression).Eval();
			return _proxy.GetBuildsByProjectCollection(parameters);
		}
	}
}
